package com.briup.pts.web.controller;

import com.briup.pts.config.auth.SecurityContext;
import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.Account;
import com.briup.pts.domain.dto.LoginUser;
import com.briup.pts.service.AccountService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
public class AuthController {

    private final AccountService service;

    @PostMapping("/login")
    public Result<?> login(@Valid LoginUser loginUser) {
        log.info("当前登录的用户为：{}", loginUser.getUsername());
        Map<String, String> map = service.login(loginUser);
        return Result.success(map);
    }

    @GetMapping("/me")
    public Result<?> me() {
        Account me = SecurityContext.currentUser();
        return Result.success(me);
    }

    @GetMapping("/logout")
    public Result<?> logout(){
        log.info("当前需要退出登录的用户为:{}",SecurityContext.username());
        return Result.success();
    }
}
